<template>
  <div id="app">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="审批人">
            <el-input v-model="formInline.user" placeholder="审批人"></el-input>
          </el-form-item>
          <el-form-item label="活动区域">
            <el-select v-model="formInline.region" placeholder="活动区域">
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" v-loading="isLoading">搜索</el-button>
            <el-button  @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-button @click="addRole">添加角色</el-button>
       <el-table
      :data="roles"
      v-loading="isLoading"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"/>
      <el-table-column
        prop="name"
        label="角色名称"/>
      <el-table-column
        prop="description"
        label="描述" />
      <el-table-column
        label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createdTime | dateFormat }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="text" @click="allocMenu(scope.row)">分配菜单</el-button>
            <el-button type="text">分配资源</el-button>
          </div>
          <div>
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="handledDelete(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 将添加与编辑单独封装到组件中 -->
    <el-dialog :title="isEdit? '编辑角色' : '添加角色'" :visible.sync="dialogFormVisible">
    <create-or-edit @success="handleSuccess" @cancle="handleCancle" :is-edit="isEdit" :roleId="roleId"></create-or-edit>
    </el-dialog>
    <div class="block">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="formInline.current"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            :disabled="isLoading"
            >
            </el-pagination>
    </div>
    </el-card>
  </div>
</template>

<script>
import CreateOrEdit from './createOrEdit.vue'
import { getRoles, deleteRole } from '@/services/role'
export default {
  components: { CreateOrEdit },
  data () {
    return {
      formInline: {
        current: 1,
        size: 10
      },
      dialogFormVisible: false,
      isLoading: false,
      roles: [],
      totalCount: 0,
      // 控制对话框的功能状态
      isEdit: false,
      // 存储正在编辑的roleId
      roleId: ''
    }
  },
  filters: {
    dateFormat (date) {
      // console.log(date)
      return date.replace('T', ' ').replace('.000+0800', '')
    }
  },
  methods: {
    allocMenu (row) {
      console.log(row)
      this.$router.push(`/role/${row.id}/alloc-menu`)
    },
    handleEdit (row) {
      this.roleId = row.id
      this.dialogFormVisible = true
      this.isEdit = true
    },
    handleCancle () {
      this.dialogFormVisible = false
    },
    handleSuccess () {
      this.dialogFormVisible = false
      this.loadRoleList()
    },
    addRole () {
      this.dialogFormVisible = true
      this.isEdit = false
    },
    handleSizeChange (val) {
      this.formInline.size = val
      this.formInline.current = 1
      this.loadRoleList()
    },
    handleCurrentChange (val) {
      this.formInline.current = val
      this.formInline.current = 1
      this.loadRoleList()
    },
    onSubmit () {
    },
    onReset () {
    },
    async loadRoleList () {
      this.isLoading = true
      const { data } = await getRoles(this.formInline)
      if (data.code === '000000') {
        this.roles = data.data.records
        this.totalCount = data.data.total
        this.isLoading = false
      }
    },
    handledDelete (row) {
      this.$confirm('确定删除吗', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发送删除请求
        const { data } = await deleteRole(row.id)
        if (data.code === '000000') {
          this.$message.success('删除成功')
        }
        this.loadRoleList()
      }).catch(() => {
        // 取消
        this.$message.info('已取消删除')
      })
    }
  },
  created () {
    this.loadRoleList()
  }
}
</script>
<style scoped>
.block{
    text-align: center;
    margin-top: 20px;
}
</style>
